<template>
    <div class="device-list">
      <!-- 顶部搜索栏 -->
      <SearchFilters @search="onSearch" />
  
      <!-- 主表：母设备、子设备、部件等 -->
      <el-table
        :data="deviceRows"
        border
        row-key="id"
        style="margin-top: 12px"
        v-loading="loading"
      >
        <!-- 展开行：备件信息 -->
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-table
              v-if="row.spareInfos?.length"
              :data="row.spareInfos"
              border
              size="small"
              style="margin-left: 40px"
            >
              <el-table-column prop="name" label="备件名称" />
              <el-table-column prop="code" label="编码" width="120" />
              <el-table-column prop="stockAvailable" label="可用库存" width="100" />
              <el-table-column prop="stockTotal" label="总库存" width="100" />
            </el-table>
            <div v-else class="ml40 text-gray-500">暂无备件</div>
          </template>
        </el-table-column>
  
        <el-table-column prop="name" label="设备名称" min-width="180" />
        <el-table-column prop="code" label="编码" width="140" />
        <el-table-column prop="beType" label="类别" width="100" />
        <el-table-column prop="area" label="区域" width="100" />
        <el-table-column prop="model" label="型号" width="120" />
        <el-table-column prop="brand" label="品牌" width="120" />
      </el-table>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { listSbtz } from '@/api/sbtz/sbtz'
  import SearchFilters from './SearchFilters.vue'
  
  const loading = ref(false)
  const deviceRows = ref([])
  
  async function onSearch(params = {}) {
    loading.value = true
    const { rows = [] } = await listSbtz({
      ...params,
      nodeTypeNotIn: 'SYSTEM',
      pageNum: 1,
      pageSize: 1000
    })
  
    // 示例：为每个设备附加备件信息
    rows.forEach(r => {
      r.spareInfos = [
        { name: '传感器', code: 'SP001', stockAvailable: 8, stockTotal: 12 },
        { name: '接头', code: 'SP002', stockAvailable: 20, stockTotal: 50 }
      ]
    })
  
    deviceRows.value = rows
    loading.value = false
  }
  
  // 页面加载自动获取
  onMounted(() => {
    onSearch()
  })
  </script>
  